package com.rs.game.minigames.duel;

import com.rs.game.World;
import com.rs.game.WorldTile;
import com.rs.game.player.Player;
import com.rs.game.player.controlers.Controler;

public class DuelControler extends Controler {

	  public static void challenge(Player target, Player player) {
			player.closeInterfaces();
			if (target == null
				|| target.hasFinished()
				|| !target.withinDistance(player, 14)
				|| !(target.getControlerManager().getControler() instanceof DuelControler)) {
			    player.getPackets().sendGameMessage(
				    "Unable to find "
					    + (target == null ? "your target" : target
						    .getDisplayName()));
			    return;
			}
			if (player.getAuraManager().isActivated()) {
			    player.getPackets().sendGameMessage("You have to deactive your aura if you want to duel.");
			    return;
			}
			if (player.getFamiliar() != null) {
			    player.getPackets().sendGameMessage("Please dismiss your familiar before starting a duel.");
			    return;
			}
			player.getTemporaryAttributes().put("DuelChallenged", target);
			player.getTemporaryAttributes().put("DuelFriendly", false);
			player.getPackets().sendGameMessage(
				"Sending " + target.getDisplayName() + " a request...");
			target.getPackets().sendDuelChallengeRequestMessage(player, false);
		    }

		    @Override
		    public boolean canPlayerOption1(final Player target) {
			player.stopAll(false);
			player.faceEntity(target);
			if (player.getInterfaceManager().containsScreenInter()
				|| player.isLocked()) {
			    player.getPackets().sendGameMessage("You're too busy.");
			    return false;
			}
			if (player.getSkills().getTotalLevel(player) < 300) {
			    player.getPackets().sendGameMessage("Your stats does not meet the requirements to duel.");
			    return false;
			}
			if (target.getTemporaryAttributes().get("DuelChallenged") == player) {
			    player.getControlerManager().removeControlerWithoutCheck();
			    target.getControlerManager().removeControlerWithoutCheck();
			    target.getTemporaryAttributes().remove("DuelChallenged");
			    player.setLastDuelRules(new DuelRules(player, target));
			    target.setLastDuelRules(new DuelRules(target, player));
			    player.getControlerManager().startControler("DuelArena", target,
				    target.getTemporaryAttributes().get("DuelFriendly"));
			    target.getControlerManager().startControler("DuelArena", player,
				    target.getTemporaryAttributes().remove("DuelFriendly"));
			    return false;
			}
			DuelControler.challenge(target, player);
			return false;
		    }

		    @Override
		    public void forceClose() {
			remove();
		    }

		    @Override
		    public boolean login() {
			start();
			return false;
		    }

		    @Override
		    public boolean logout() {
			return false;
		    }

		    @Override
		    public void magicTeleported(int type) {
			player.getPackets().closeInterface(
				player.getInterfaceManager().hasRezizableScreen() ? 11 : 0);
			removeControler();
			remove();
		    }

		    @Override
		    public void moved() {
			if (player.getRegionId() != 13363) {
			    removeControler();
			    remove();
			}
		    }

		    @Override
		    public boolean processItemTeleport(WorldTile toTile) {
			player.getPackets().closeInterface(
				player.getInterfaceManager().hasRezizableScreen() ? 11 : 0);
			return true;
		    }

		    @Override
		    public boolean processMagicTeleport(WorldTile toTile) {
			player.getPackets().closeInterface(
				player.getInterfaceManager().hasRezizableScreen() ? 11 : 0);
			return true;
		    }

		    public void remove() {
			player.getPackets().closeInterface(
				player.getInterfaceManager().hasRezizableScreen() ? 11 : 0);
			player.getAppearence().generateAppearenceData();
			player.getPackets().sendPlayerOption("null", 1, false);
		    }

		    public void sendDuelConfigs(final Player target) {
			if (target == null)
			    return;
			if (target.getInterfaceManager().containsScreenInter()
				|| target.isLocked()) {
			    player.getPackets().sendGameMessage("The other player is busy.");
			    return;
			}
			if (target.getSkills().getTotalLevel(target) < 300) {
			    player.getPackets().sendGameMessage("Your opponent does not meet the requirements to duel.");
			    return;
			}
			if (player.getX() == target.getX() && player.getY() == target.getY()) {
			    if (!player.addWalkSteps(player.getX() - 1, player.getY(), 1))
				if (!player.addWalkSteps(player.getX() + 1, player.getY(), 1))
				    if (!player.addWalkSteps(player.getX(), player.getY() + 1,
					    1))
					player.addWalkSteps(player.getX(), player.getY() - 1, 1);
			}
			if (!World.canMoveNPC(player.getPlane(), player.getX(), player.getY(),
				1)) {
			    player.getPackets().sendGameMessage("Unable to stake here.");
			    return;
			}
			if (!target.withinDistance(player, 14)) {
			    player.getPackets().sendGameMessage(
				    "Unable to find target " + target.getDisplayName());
			    return;
			}
			if (target.getTemporaryAttributes().get("DuelChallenged") == player) {
			    player.getControlerManager().removeControlerWithoutCheck();
			    target.getControlerManager().removeControlerWithoutCheck();
			    target.getTemporaryAttributes().remove("DuelChallenged");
			    player.setLastDuelRules(new DuelRules(player, target));
			    target.setLastDuelRules(new DuelRules(target, player));
			    player.getControlerManager().startControler("DuelArena", target,
				    target.getTemporaryAttributes().get("DuelFriendly"));
			    target.getControlerManager().startControler("DuelArena", player,
				    target.getTemporaryAttributes().remove("DuelFriendly"));
			    return;
			}
			player.getTemporaryAttributes().put("WillDuelFriendly", false);
			player.faceEntity(target);
		    }

		    @Override
		    public void sendInterfaces() {
			if (player.getRegionId() == 13363) {
			    player.getInterfaceManager().setOverlay(1362,
				    player.getInterfaceManager().hasRezizableScreen());
			}
		    }

		    @Override
		    public void start() {
			sendInterfaces();
			player.getPackets().sendPlayerOption("Challenge", 1, false);
			player.getAppearence().generateAppearenceData();
			moved();
		    }
		}
